<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/laypage.css">
<script type="text/javascript" src="js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="js/laypage/laypage.js"></script>
<script type="text/javascript" src="js/layer/layer.js"></script>
<script type="text/javascript" src="js/vue/vue.min.js"></script>
<script src="http://echarts.baidu.com/dist/echarts.min.js"></script>
<body>
<div id="app" class="container">
    <form class="form-inline bg-danger" role="form">
        查询条件：
        <div class="form-group">
            <label class="sr-only" for="username">用户名称</label>
            <input type="text" class="form-control" id="username" placeholder="用户名称">
        </div>
        <div class="form-group">
            <label class="sr-only" for="userage">用户年龄</label>
            <input type="text" class="form-control" id="userage" placeholder="用户年龄">
        </div>
        <button type="button" id="findUser" class="btn btn-success">查询用户</button>
        <button type="button" id="addUserBtn" class="btn btn-danger">增加用户</button>
        <button type="button"  class="btn btn-danger" style="background-color: #2e8ded; float: right" @click="imports()">导出报表</button>
        <button type="button"  class="btn btn-danger" style="background-color: #2e8ded; float: right" @click="exports()">导入报表</button>
        <button type="button"  class="btn btn-danger" style="background-color: #2e8ded; float: right" @click="expo()">导入</button>
    </form>

    <div class="table-responsive">
        <table class="table">
            <thead>
            <tr class="success">
                <td>序号</td>
                <td>用户</td>
                <td>年龄</td>
                <td>操作</td>
            </tr>
            </thead>

            <tbody>
            <tr class="active" v-for="(item,index) in result">
                <td>{{index+1}}</td>
                <td>{{item.name}}</td>
                <td>{{item.age}}</td>
                <td><a href="#" @click="editEvent(item.id)">编辑</a>|<a href="#" @click="delEvent(item.id)">删除</a></tr>


            </tbody>
        </table>
        <div id="pagenav"></div>
    </div>
</div>

<!-- 为ECharts准备一个具备大小（宽高）的Dom -->
<div id="bar" style="width: 600px;height:400px; float:left;" ></div>
<div id="pie" style="width: 500px;height:300px; float:right;"></div>
</body>
</html>
<script>
    var letAge = '';
    var betAge = '';
    var app = new Vue({
        el: '#app',
        data: {
            result: []
        }
    });
    var getUserPageList = function (curr) {
        $.ajax({
            type: 'GET',
            dataType: 'json',
            async:false,
            url: '/person/getLikePerson.do',
            data: {
                pageNum: curr || 1,
                pageSize: 3,
                name: $("#username").val(),
                age: $("#userage").val()
            },
            success: function (msg) {
                letAge = msg.letAge;
                betAge = msg.betAge;

                app.result = msg.page.result;
                laypage({
                    cont: 'pagenav',
                    pages: msg.page.totalPage,
                    skip: true,
                    skin: '#7B68EE',
                    curr: curr || 1,
                    jump: function (obj, first) {
                        if (!first) {
                            getUserPageList(obj.curr);
                        }
                    }
                });





            }
        });
    }

    getUserPageList();

    $("#findUser").on("click", function(){
        getUserPageList();
    })

    $("#addUserBtn").on("click", function () {

        layer.open({
            type:2,
            title:'新增用户',
            fix: false,
            maxmin:true,
            shadeColse:true,
            area:['800px', '500px'],
            content: 'add_person.jsp',
            end:function(){
                getUserPageList();
            }

        });
    });
    var expo = function () {
        window.location.href="/person/export.do";
    }
    var exports =function () {
        window.location.href="/import.jsp";
    }
    var imports = function () {
        layer.confirm('确定导出？', {
            btn:['是', '否']
        },function () {
            //是
            window.location.href="/person/export.do"
            layer.msg('导出成功', {icon:6});

            //否
        },function () {

        });
    }

    var delEvent = function (id) {
        layer.confirm('确定删除？', {
            btn:['是', '否']
        },function () {
            //是
            $.ajax({
                type:'GET',
                dataType:'json',
                url:'/person/delPerson.do',
                data:{
                    id:id
                },
                success:function (msg) {
                    getUserPageList();
                    layer.msg('删除成功'+id, {icon:6});
                }
            });
            //否
        },function () {

        });
    }

    //编辑用户
    var editEvent = function (id) {
        layer.open({
            type:2,
            title:'编辑用户',
            fix: false,
            maxmin:true,
            shadeClose:true,
            area:['800px', '500px'],
            content:'/person/findPerson.do?id='+id,
            end:function () {
                getUserPageList();
            }



        });
    }

    echart();
    function echart() {
         //加载柱状图特效
        // 基于准备好的dom，初始化echarts实例
        var myBarChart = echarts.init(document.getElementById('bar'));
        // 指定图表的配置项和数据
        var option = {
            title: {
                text: 'ECharts 入门示例'
            },
            tooltip: {
                triggerEvent:true},
            legend: {
                data:['人数',"趋势"]
            },
            xAxis: {
                data: ["大于20岁","小于20岁"],
                triggerEvent:true,  //可以点击x轴坐标,
                axisTick: {
                    alignWithLabel: true //坐标值是否在刻度中间
                }
            },
            yAxis: {
                type: 'value',
                splitArea: {
                    show: true
                },
                //百分比显示Y轴
                axisLabel: {
                    show: true,
                    interval: 'auto'
                },
                show: true,
                triggerEvent:true //可以点击y轴坐标
            },
            series: [{
                name: '人数',
                type: 'bar',
                data: [letAge, betAge],
                itemStyle: {
                    normal: {
                        label: {
                            show: true,
                            position: 'top'
                        }
                    }
                }
            },{
                name: '趋势',
                type: 'line',
                data: [letAge, betAge],
                smooth:false,   //是否为曲线,默认为false
                itemStyle:{
                    normal:{
                        lineStyle:{
                            width:1,       // 虚线的宽度
                            type:'dotted'  //'dotted'虚线 'solid'实线
                        }
                    }
                }
            }]
        };
        // 使用刚指定的配置项和数据显示图表。
        myBarChart.setOption(option);
        myBarChart.on('click', function (params) {
            alert("单击了"+params.componentType+"x轴标签"+params.value);
            if(params.componentType == "xAxis"){
                alert("单击了"+params.value+"x轴标签"+params.name);
            }else{
                alert("单击了"+params.name+"柱状图"+params.value);
            }
        });


        //加载饼状图
        var myPieChart = echarts.init(document.getElementById('pie'));
        // 指定图表的配置项和数据
        var option = {
            "legend":{
                "orient":"vertical",
                "left":"left"
            },
            "series":[
                {
                    "data":[
                        {
                            "name":"小于20岁",
                            "value":letAge
                        },
                        {
                            "name":"大于20岁",
                            "value":betAge
                        }

                    ],
                    "center":[
                        "50%",
                        "60%"
                    ],
                    "name":"访问来源",
                    "itemStyle":{
                        "normal":{
                            "label":{
                                "formatter":"{b}\n{c}\n{d}%",
                                "show":true
                            }
                        },
                        "emphasis":{
                            "shadowOffsetX":0,
                            "shadowBlur":10,
                            "shadowColor":"rgba(0, 0, 0, 0.5)"
                        }
                    },
                    "radius":"55%",
                    "type":"pie"
                }
            ],
            "tooltip":{
                "formatter":"{a} <br/>{b} : {c} ({d}%)",
                "trigger":"item"
            },
            "title":{
                "subtext":"纯属虚构",
                "x":"center",
                "text":"某站点人数年龄分布"
            }
        };
        // 使用刚指定的配置项和数据显示图表。
        myPieChart.setOption(option);
        myPieChart.on("click", pieConsole);

        function pieConsole(param) {
            alert(option.series[0].data.length);
            alert(param.value);
            alert(param.name);
            alert(option.series[param.seriesIndex].data[param.dataIndex].value);
            alert(option.series[param.seriesIndex].data[param.dataIndex].name);
        }
    }


</script>

